package cn.usts.edu.lesson01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 练习Thread
 * 实现多线程同时下载
 * */
public class ThreadImageDownload extends Thread{
    String file;
    String url;
    String filename;

    public ThreadImageDownload(String file, String url) {
        this.file = file;
        this.url = url;
    }

    @Override
    public void run() {
        ImageDownload imageDownload = new ImageDownload();
        imageDownload.downLoad(url,file);
        filename = file.split("\\\\")[11];//获取文件名
        System.out.println(filename+"下载完成");
    }

    public static void main(String[] args) {
        ThreadImageDownload t1 = new ThreadImageDownload(
                "D:\\all_projects\\java_projects\\java_ij\\springMVC\\ThreadStudy\\src\\cn\\usts\\edu\\lesson01\\a.jpg"
                ,"https://bbs.qn.img-space.com/202110/18/1c3eb5a6f1d59adaa9b608c7da64d95a.jpg");
        ThreadImageDownload t2 = new ThreadImageDownload(
                "D:\\all_projects\\java_projects\\java_ij\\springMVC\\ThreadStudy\\src\\cn\\usts\\edu\\lesson01\\b.jpg"
                ,"https://bbs.qn.img-space.com/202110/18/6d254e56adf9094cc003c815d3d6d3e1.jpg");
        ThreadImageDownload t3 = new ThreadImageDownload(
                "D:\\all_projects\\java_projects\\java_ij\\springMVC\\ThreadStudy\\src\\cn\\usts\\edu\\lesson01\\c.jpg"
                ,"https://bbs.qn.img-space.com/202110/18/72d862d6074bb2edc1e6df61ce78455f.jpg"
        );

        t1.start();
        t2.start();
        t3.start();
    }
}

class  ImageDownload{

    public void downLoad(String url,String file)  {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(file));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("downLoad()方法出错");
        }
    }
}
